package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Resource
    TeachplanMapper teachplanMapper;
    @Resource
    TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    @Transactional
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
//        根据是否传入ID来判断是增加还是修改
        if (teachplanDto.getId() == null) {//增加
//            是增加章,还是小结  增加章只用改变teachplan这个表  不管是增加章,还是小结,都要进行的操作,小结有额外操作
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto, teachplan);
            teachplan.setCreateDate(LocalDateTime.now());
            teachplan.setChangeDate(LocalDateTime.now());
//            Integer orderby = teachplanMapper.selectById(teachplanDto.getId()).getOrderby();//新增无ID
//            teachplan.setOrderby(orderby + 1);//todo 不能这样加,万一加在中间,那么就会冲突,黑马的方式也不对啊??
//            todo 新方式??
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            Long parentId = teachplan.getParentid();
            Long courseId = teachplanDto.getCourseId();
            queryWrapper.eq(Teachplan::getCourseId,courseId);
            queryWrapper.eq(Teachplan::getParentid,parentId);
            Integer count = teachplanMapper.selectCount(queryWrapper);
            teachplan.setOrderby(count + 1);
            teachplanMapper.insert(teachplan);
//                orderby???  不会涉及到媒资
//            if (teachplanDto.getGrade().equals(2)){ //Integer包装类要用equals证明相等,增加章
//                TeachplanMedia teachplanMedia = new TeachplanMedia();
//                teachplanMedia.setTeachplanId(teachplan.getId());
//                teachplanMedia.setCourseId(teachplanDto.getCourseId());
//                teachplanMedia.setCreateDate(LocalDateTime.now());
////           课程发布标识没有,媒资文件原始名称
//                teachplanMediaMapper.insert(teachplanMedia);
//            }
        }else {//修改
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto, teachplan);
            teachplan.setChangeDate(LocalDateTime.now());
            teachplanMapper.updateById(teachplan);
//            不会涉及到媒资
//            if (teachplanDto.getGrade().equals(2)){
//                TeachplanMedia teachplanMedia = new TeachplanMedia();
//            }
        }
    }

    @Override
    public void deleteTeachplan(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan.getGrade().equals(1)){//删除第一级别的章时要求章下边没有小节方可删除。
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid,id);
//            if (teachplanMapper.selectCount(queryWrapper).equals(0)){//没有小节
//                teachplanMapper.deleteById(id);
//            }else {
//                todo 还要返回"errCode":"120409"
//                XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
//            }
            if (teachplanMapper.selectCount(queryWrapper) > 0){//有小节
                XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
            }
        }
        teachplanMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void upOrderby(Long id) {//区分是章节还是子节点,因为所找出被替换的节点where条件方式不一样
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer orderby = teachplan.getOrderby();
        if (orderby.equals(1)){// 如果是1,说明他原本就是第一个,无法上移
            XueChengPlusException.cast("第一个,无法上移");
        }
//         先把原本在上面的课程计划,即要替换的课程改到下面
//        步骤不能反,因为先上移的话,两个排序一样,到时候无法获取被替换的那个ID,令他下移
        LambdaQueryWrapper<Teachplan> upQueryWrapper = new LambdaQueryWrapper<>();
        if (teachplan.getGrade().equals(1)){//是章节
            upQueryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());//子节点的课程ID跟父节点一样
            upQueryWrapper.eq(Teachplan::getGrade,teachplan.getGrade());
        }else {//子节点
            upQueryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        }
        upQueryWrapper.eq(Teachplan::getOrderby,orderby - 1);//通过orderby和parent来确定
        Teachplan oldTeachplan = teachplanMapper.selectOne(upQueryWrapper);
        oldTeachplan.setOrderby(orderby);
        teachplanMapper.updateById(oldTeachplan);
//      移上去
        teachplan.setOrderby(orderby - 1);
        teachplanMapper.updateById(teachplan);
    }

    @Override
    public void downOrderby(Long id) {//区分是章节还是子节点
        Teachplan teachplan = teachplanMapper.selectById(id);
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        Integer integer = teachplanMapper.selectCount(queryWrapper);//总共几个子节点
        Integer orderby = teachplan.getOrderby();
        if (orderby.equals(integer)){// 如果相等,说明他原本就是最后一个,无法下移
            XueChengPlusException.cast("最后一个,无法下移");
        }
//         先把原本在上面的课程计划,即要替换的课程改到上面
//        步骤不能反,因为先下移的话,两个排序一样,到时候无法获取被替换的那个ID,令他上移
        LambdaQueryWrapper<Teachplan> downQueryWrapper = new LambdaQueryWrapper<>();
        if (new Integer(1).equals(teachplan.getGrade())){//是章节
            downQueryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());
            downQueryWrapper.eq(Teachplan::getGrade,teachplan.getGrade());
        }else {//子节点
            downQueryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        }
        downQueryWrapper.eq(Teachplan::getOrderby,orderby + 1);//通过orderby和parent来确定
        Teachplan oldTeachplan = teachplanMapper.selectOne(downQueryWrapper);
        oldTeachplan.setOrderby(orderby);
        teachplanMapper.updateById(oldTeachplan);
//      移下去
        teachplan.setOrderby(orderby + 1);
        teachplanMapper.updateById(teachplan);
    }

    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if (teachplan == null){
            XueChengPlusException.cast("课程计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengPlusException.cast("只允许第二级教学计划绑定媒资文件");
        }
        Long courseId = teachplan.getCourseId();
//        先删除绑定的数据(可以直接修改啊)
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getMediaId,bindTeachplanMediaDto.getMediaId()));
//                在添加
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
    }

    @Override
    public Teachplan getTeachplanisFree(Long teachplanId) {
        return teachplanMapper.selectById(teachplanId);
    }
}
